package com.liang.lang.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.liang.log.Log;

/**
 * @author pjt 字符串工具
 */
public class StringUtils {

	private String oldEncoding;
	private String newEncoding;

	public StringUtils() {

	}
	
	public StringUtils(String oldEncoding, String newEncoding) {
		this.oldEncoding = oldEncoding;
		this.newEncoding = newEncoding;
	}

	/**
	 * @param oldString
	 * @param oldEncoding
	 * @param newEncoding
	 * @return
	 */
	public static String transcoding(String oldString, String oldEncoding,
			String newEncoding) {
		if(null == oldString)
			return null;
		try {
			if(null == oldEncoding){
				return new String(oldString.getBytes(), newEncoding);
			}
			URLEncoder.encode(oldString, oldEncoding);
			return new String(oldString.getBytes(oldEncoding), newEncoding);
		} catch (Exception e) {
			e.printStackTrace();
			Log.log(new UnsupportedEncodingException("转码出错：检查是否存在您设定的字符集"));
			return oldString;
		}
	}

	public String transcoding(String oldString) {
		if (null != newEncoding) {
			return transcoding(oldString, oldEncoding, newEncoding);
		}else{
			Log.log(new Exception("没有实例化字符集"));
			return oldString;
		}
	}
}
